<?php
use think\facade\Session;
use think\facade\Cache;
use app\store\model\Store;
use app\store\model\StoreGroup;
use app\store\model\StoreRule;
use app\store\model\StoreLog;
use app\store\model\DictData;
use app\store\model\User;

/**
 * 权限验证
 * @param $url
 * @return bool
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\DbException
 * @throws \think\db\exception\ModelNotFoundException
 */
function auth_url($url)
{
    //检查管理员
    $store = Store::where('id',StoreId())->cache('store_info'.StoreId(),86400,'store'.StoreId())->find();
    if (!(new \core\StoreAuth())->check($url, Session::get('user_auth.user_id')) && $store['admin_id'] != UserId()) {
        return false;
    }
    return true;
}
/**
 * 检测会员是否登录
 * @return integer 0/管理员ID
 */
function is_user_login()
{
    $user = Session::get('user_auth');
    if (empty($user)) {
        return 0;
    } else {
        return Session::get('user_auth_sign') == data_auth_sign($user) ? $user['user_id'] : 0;
    }
}
/**
 * 保存前台用户行为
 * @param string $remark 日志备注
 */
function insert_user_log($remark)
{
    if (session('?user_auth')) {
        StoreLog::create([
            'store_id'     => Session::get('user_auth.store_id'),
            'client'     => request()->isMobile()?'1':'0',
            'user_id'     => Session::get('user_auth.user_id'),
            'ip'          => request()->ip(),
            'url'         => request()->url(true),
            'method'      => request()->method(),
            'type'        => request()->type(),
            'param'       => json_encode(request()->param()),
            'remark'      => $remark
        ]);
    }
}
//获取用户ID
function UserId()
{
    $info = session('user_auth.user_id');
    return $info;
}
//获取商家ID
function StoreId()
{
    $info = session('user_auth.store_id');
    return $info;
}
/**
 * 字典列表
 * @param string $dict_id 字典类别ID
 */
function get_dict_list($dict_id)
{
    $list = DictData::field('id,id as value,store_id,dict_id,name,is_default')->where('dict_id',$dict_id)->cache('dict_list'.$dict_id.StoreId(),86400,'store'.StoreId())->where('status','1')->order('sort_order desc,id desc')->select();
    return $list;
}
/**
 * 字典列表
 * @param string $id 字典ID
 */
function get_dict_value($id)
{
    $list = DictData::where('id',$id)->value(['name']);
    return $list;
}
/**
 * 获取默认值
 * @param string $id 字典ID
 */
function default_value($dict_id)
{
    $list = DictData::where('dict_id',$dict_id)->where('is_default',1)->value('id');
    return $list;
}

/**
 * 获取我所在的权限
 * @param string $id 字典ID
 */
function my_auth()
{
    $myauth = cache('user_auth'.UserId());
    if(empty($myauth)){
        $groupid = StoreGroup::get_group_list();
        //得到所有子部门的ID
        $myauth = User::where('group_id','in',$groupid)->cache('user_auth'.UserId(),86400,'store'.StoreId())->column('id');
    }
    return $myauth;
}
/**
 * 获取我的角色所在的权限
 */
function my_rule()
{
    //查询我是不是主管理员
    $store = Store::where('id',StoreId())->cache('store_info'.StoreId(),86400,'store'.StoreId())->find();
    if($store['admin_id'] == UserId()){
        $myrule = \app\common\model\Taocan::where('id',$store['taocan'])->value('rules');
    }else{
        //获取我的角色ID
        $ruleIds = User::where('id',UserId())->cache('user_rule_id'.UserId(),86400,'store'.StoreId())->where('id',UserId())->value('rule_id');
        $rules = StoreRule::where('id','in',$ruleIds)->cache('user_rule'.UserId(),86400,'store'.StoreId())->column('rules');
        $myrule = implode(',',$rules);
    }
    return $myrule;
}

/**
 * 清空所有的权限缓存
 * @param string $user_id 用户ID
 */
function clear_store_cache()
{
    //检查是否传入USERID
    Cache::tag('store'.StoreId())->clear();
    return ;
}
/**
 * 我的商户资料
 */
function shopinfo(){
    $data = Store::where('id',StoreId())->cache('shop_info'.StoreId(),86400,'store'.StoreId)->find();
    return $data;
}